require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, $, coAjax, QueryString, href, hash_routie) {
	//INIT
	var CARD_FACTORY_TYPE_array = [];
	var qs;
	var _card_factory_id;
	jSouper.forEach(appConfig.CARD_FACTORY_TYPE, function(value, key) {
		CARD_FACTORY_TYPE_array.push({
			key: key,
			value: value
		});
	});
	App.set("$Cache.CARD_FACTORY_TYPE_array", CARD_FACTORY_TYPE_array);

	hash_routie.on("card_factory", function() {
		coAjax.get(appConfig.bus.card_factory_list, function(result) {
			App.set("$Cache.bus.card_factory.list", result.result)
		});

		qs = new QueryString(location.hash);
		_card_factory_id = qs.get("card_factory_id");
		if (_card_factory_id) {
			coAjax.get(appConfig.card_factory.by_id + _card_factory_id, {
				bus_id: App.get("bus_loginer._id")
			}, function(result) {
				var card_factory = result.result;
				App.set("$Cache.bus.card_factory.new", result.result);
				if (!(App.get("$Cache.bus.card_factory.new.info.classify_limit") instanceof Array)) {
					App.set("$Cache.bus.card_factory.new.info.classify_limit", []);
				}
			});
			var classify_tree = appConfig.bus.c_f_c.classify_tree.replace(":card_factory_id", _card_factory_id);
			coAjax.get(classify_tree, function(result) {
				App.set("$Cache.bus.card_factory.classify_tree", result.result);
			});
		} else {
			App.set("$Cache.bus.card_factory.new", {
				info: {
					classify_limit: []
				}
			});
			App.set("$Cache.bus.card_factory.classify_tree", null);
		}
	});
	App.set("$Event.bus.card_factory.add_classify", function() {
		//数据整理，删除重复项
		App.set("$Cache.bus.card_factory.new.info.classify_limit", $.unique(App.get("$Cache.bus.card_factory.new.info.classify_limit")));
	});

	App.set("$Event.bus.card_factory.add_card_factory", function(e) {
		coAjax.post(appConfig.bus.card_factory_add, App.get("$Cache.bus.card_factory.new.info"), function(result) {
			alert("会员卡添加成功");
			var card_factory_list = App.get("$Cache.bus.card_factory.list");
			card_factory_list.unshift(result.result);
			App.set("$Cache.bus.card_factory.list", card_factory_list)
		});
	});

	App.set("$Event.bus.card_factory.update_card_factory", function(e) {
		var card_factory = App.get("$Cache.bus.card_factory.new")
		coAjax.put(appConfig.bus.card_factory_update + card_factory._id, card_factory, function(result) {
			alert("会员卡修改成功");
			// App.get("$Event.bus.card_factory.cancel_update")();
			hash_routie.emit("card_factory");
		});
	});
	App.set("$Event.bus.card_factory.cancel_update", function(e) {
		hash_routie.hash({}, true);
	});
	App.set("$Event.bus.card_factory.to_update", function(e, vm) {
		App.set("$Cache.bus.card_factory.new", vm.get());
		hash_routie.hash({
			card_factory_id: vm.get("_id")
		});
	});

	App.set("$Event.bus.card_factory.remove_card_factory", function(e, vm) {
		myConfirm("确定要删除这个会员卡？", function() {
			coAjax["delete"](appConfig.bus.card_factory_remove + vm.get("_id"), function(result) {
				alert("会员卡删除成功");
				App.set("$Cache.bus.card_factory.list", result.result)
			});
		});
	});
});